﻿using System.Web;
using System.Web.Script.Serialization;
using CMS.Model;
using Core.Library;
using DataObject.QA;

namespace CMS.Handlers
{
    /// <summary>
    /// Summary description for PreviewHandler
    /// </summary>
    public class PreviewHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "qa":
                    QA(context);
                    break;
            }
        }

        public void QA(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = ConvertUtility.ConvertTo<int>(context.Request[Utils.Id]);

                var o = QAController.QA_GetById(id);
                if (o != null)
                {
                    var result = QAController.Answer_GetListByReplyTo(id);
                    var viewModel = new QAModel
                    {
                        QA_Text = o.QA_Text,
                        QA_CreatedDate = DateTimeFunctions.FormatIn_HHMM_DDMMYYYY(o.QA_CreatedDate),
                        lstAnswer = result
                    };

                    var objSerial = new JavaScriptSerializer();
                    var json = objSerial.Serialize(viewModel);
                    context.Response.Write(json);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}